/*
 * Created on 23/ago/07
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.mmhc.com;

import it.unibo.deis.lia.networkInterface.*;
import it.unibo.deis.lia.mmhc.util.*;
import it.unibo.deis.lia.location.*;

import java.util.*;

public class SimpleBottomMetric implements BottomMetric {

	private float enduranceThreshold;

	public SimpleBottomMetric(float enduranceThreshold) {
		// inserire i requisiti
		this.enduranceThreshold = enduranceThreshold;
	}

	public Vector<Connector> evaluate(int mobilityState,
			Vector<Connector> availableConnectors) throws Exception {
		Vector<Connector> res = new Vector<Connector>();
		// System.out.println("SimpleBottomMetric: mobilityState "+mobilityState);
		System.out.println("\tSimpleBottomMetric: availableConnectors "
				+ availableConnectors);
		Hashtable<GenericNetworkInterface, Connector> singleConectorInterfaces = new Hashtable<GenericNetworkInterface, Connector>();

		for (int i = 0; i < availableConnectors.size(); i++) {
			Connector connector = availableConnectors.elementAt(i);
			float endurance = connector.getEnduranceEstimation();
			// System.out.println("\tevaluating #"+i+" "+connector+" endurance:"+endurance);
			if (endurance >= enduranceThreshold) {
				GenericNetworkInterface locaNetworkInterface = connector
						.getLocalNetworkInterface();
				connector.setOverallValue(endurance);
				if (connector.isMultipleChannels()) {
					// Connector tempConnector=new
					// Connector(locaNetworkInterface,connector.getConnectorDescription(),endurance,endurance);
					// connector.setOverallValue(endurance);
					// System.out.println("\t\tmultiple "+connector);
					// res.addElement(tempConnector);
					res.addElement(connector);
				} else {
					// only one connector per time for this type of interface
					Connector previousConnector = singleConectorInterfaces
							.get(locaNetworkInterface);
					// System.out.println("\t\tsingle temp "+previousConnector);
					if (previousConnector == null) {
						// Connector tempConnector=new
						// Connector(locaNetworkInterface,connector.getConnectorDescription(),endurance,endurance);
						// System.out.println("\t\tsingle NEW "+connector);
						// singleConectorInterfaces.put(locaNetworkInterface,
						// tempConnector);
						singleConectorInterfaces.put(locaNetworkInterface,
								connector);
					} else if (previousConnector.getOverallValue() < endurance) {
						// Connector tempConnector=new
						// Connector(locaNetworkInterface,connector.getConnectorDescription(),endurance,endurance);
						// System.out.println("\t\tsingle ANOTHER endurance "+connector);
						// singleConectorInterfaces.put(locaNetworkInterface,
						// tempConnector);
						singleConectorInterfaces.put(locaNetworkInterface,
								connector);
					} else if (previousConnector.getOverallValue() == endurance) {
						if (((LocationOss) previousConnector
								.getConnectorDescription()).getValue() < ((LocationOss) connector
								.getConnectorDescription()).getValue()) {
							// Connector tempConnector=new
							// Connector(locaNetworkInterface,connector.getConnectorDescription(),endurance,endurance);
							// System.out.println("\t\tsingle ANOTHER rssi "+connector);
							// singleConectorInterfaces.put(locaNetworkInterface,
							// tempConnector);
							singleConectorInterfaces.put(locaNetworkInterface,
									connector);
						}
					}
				}
			}
		}

		Enumeration<GenericNetworkInterface> keys = singleConectorInterfaces
				.keys();
		while (keys.hasMoreElements()) {
			Connector connector = singleConectorInterfaces.get(keys
					.nextElement());
			res.addElement(connector);
		}

		return res;
	}

}
